package dfbz.com.dao;

import dfbz.com.dao.impl.BaseDaoImpl;
import dfbz.com.util.JDBCUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

public class FavoriteDao extends BaseDaoImpl {


    public void addFavorite(int aId, int mId) {
        QueryRunner qr = new QueryRunner(JDBCUtil.getDataSource());
        String sql="insert into favorite(u_id,a_id) values(?,?)";
        try {
            qr.update(sql,mId,aId);
        } catch (SQLException e) {
            e.printStackTrace();
        }


    }


    public Integer findTotalCollects(Object aId) {//传入文章id
        Integer total = 0;
        QueryRunner qr = new QueryRunner(JDBCUtil.getDataSource());
        Map<String, Object> map=null;
        try {

                String sql = "select count(*) as total from favorite where a_id=?";
                map = qr.query(sql, new MapHandler(),aId);
                total = Integer.parseInt(map.get("total").toString());


        } catch (SQLException e) {
            e.printStackTrace();
        }
        return total;
    }


    public List<Map<String, Object>> findFavoriteCountList() {
        QueryRunner qr = new QueryRunner(JDBCUtil.getDataSource());
        List<Map<String, Object>> map=null;
        try {
            String sql = "select count(*) as total,a_id as aId\n" +
                    "from favorite\n" +
                    "GROUP BY a_id order by a_id desc";
            map = qr.query(sql, new MapListHandler());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return map;
    }

    public long findMyFavoriteArticleCount(Integer userId) {
        QueryRunner qr = new QueryRunner(JDBCUtil.getDataSource());
        Long count=0L;
        Map<String, Object> map=null;
        try {
            String sql = "select count(*) as total\n" +
                    "from favorite\n" +
                    "where u_id=?";
            map = qr.query(sql,new MapHandler() ,userId);
            count = Long.parseLong(map.get("total").toString());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;

    }

    public boolean isExist(int aId, int mId) {
        QueryRunner qr = new QueryRunner(JDBCUtil.getDataSource());
        Long count=0L;
        Map<String, Object> map=null;
        try {
            String sql = "select count(*) as total\n" +
                    "from favorite\n" +
                    "where u_id=? and a_id=? ";
            map = qr.query(sql,new MapHandler() ,mId,aId);
            count = Long.parseLong(map.get("total").toString());

        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (count==0){
            return false;
        }else {
            return true;
        }

    }

    public void removeFavorite(int aId, int uId) {

        QueryRunner qr = new QueryRunner(JDBCUtil.getDataSource());
        String sql="delete from favorite  where a_id=? and u_id=?";
        System.out.println("sql:"+sql);
        try {
            qr.update(sql,aId,uId);
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
}
